#include "filebackupdialog.h"
#include "ui_filebackupdialog.h"
#include <QDebug>
#include <QFileDialog>
#include <QFileInfo>
#include <QStandardItemModel>
#include <QTableWidgetItem>
#include "hmicommondefine.h"
#include "instructionEnum.h"


FileBackUpDialog::FileBackUpDialog(GlobalData *globalDataIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FileBackUpDialog)
{
    globalData=globalDataIn;
    move(0,0);
    ui->setupUi(this);
    setStyleSheet("background-color:white;");

    //uDiskLocation = USB_DISK_ADDRESS;
    //hardDiskLocation = HARD_DISK_ADDRESS;
    //backupLocation = hardDiskLocation;
    backupLocation = CONTROLLER_HARD_DISK_ADDRESS;

    ui->tableWidget->setRowCount(robotList.size());
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setColumnWidth(0,30);
    ui->tableWidget->setColumnWidth(1,170);
    ui->tableWidget->setColumnWidth(2,210);
    ui->tableWidget->verticalHeader()->hide();
    QStringList header;
    header<<tr("")<<tr("机器人ID")<<tr("机器人名称");
    ui->tableWidget->setHorizontalHeaderLabels(header);
    for(int n=0;n<robotList.size();n++)
    {

    ui->tableWidget->setItem(n,0,new QTableWidgetItem());
    ui->tableWidget->setItem(n,1,new QTableWidgetItem());
    ui->tableWidget->setItem(n,2,new QTableWidgetItem());
    }
    QTableWidgetItem *checkBox = new QTableWidgetItem();
    checkBox->setCheckState(Qt::Unchecked);
    checkBox->setText("");
    ui->tableWidget->setItem(0,0, checkBox);

//    connect(ui->tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(cellChangedSlot(int, int)));

    robot.resize(0);

#ifdef BUILD_ARM
    setGeometry(70,128,460,595);
#endif

    //
    globalData = NULL;
}

FileBackUpDialog::~FileBackUpDialog()
{
    delete ui;
}

void FileBackUpDialog::on_returnBtn_clicked()
{
    this->hide();
}

void FileBackUpDialog::cellChangedSlot(int row, int column)
{
    if(ui->tableWidget ->item(row, column)->checkState() == Qt::Checked)
    {
        qDebug()<<"true";
        RobotBackUpInformation robotInfo;
        robotInfo.RobotId=ui->tableWidget->item(row, column+1)->text().toInt();
        robotInfo.RobotName=ui->tableWidget->item(row, column+2)->text();
        robot.append(robotInfo);
        qDebug()<<robot.size();
    }
    else
    {
        qDebug()<<"false";
        if(robot.size()>1)
        {
            for (int i=1;i<robot.size();i++)
            {
                if( i == ui->tableWidget->item(row, column+1)->text().toInt())
                {
                    qDebug()<<"ok";
                    robot.erase(robot.begin()+i);
                    qDebug()<<robot.size();
//                    break;
                }
            }
        }
        else
        {
            robot.clear();
            qDebug()<<robot.size();
        }
    }
}

void FileBackUpDialog::on_backup_clicked()
{
    if(ui->lineEdit->text() == "")
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("请点击浏览按钮获取备份的路径"));
        return;
    }
    int allFlag;
    int userProgramFlag;
    int coordinateFlag;
    int systemConfigFlag;
    int robotConfigFlag;
    if(ui->checkBox->checkState()==Qt::Checked)
    {
       allFlag=1;
    }
    else
    {
        allFlag=0;
    }
    if(ui->checkBox_2->checkState()==Qt::Checked)
    {
       userProgramFlag=1;
    }
    else
    {
        userProgramFlag=0;
    }
    if(ui->checkBox_3->checkState()==Qt::Checked)
    {
       coordinateFlag=1;
    }
    else
    {
        coordinateFlag=0;
    }
    if(ui->checkBox_4->checkState()==Qt::Checked)
    {
        robotConfigFlag=1;
    }
    else
    {
        robotConfigFlag=0;
    }
    if(ui->checkBox_5->checkState()==Qt::Checked)
    {
       systemConfigFlag=1;
    }
    else
    {
        systemConfigFlag=0;
    }
    emit backupFile(allFlag,userProgramFlag,coordinateFlag,robotConfigFlag,systemConfigFlag,robot,backupLocation);
}

void FileBackUpDialog::on_exployer_clicked()
{
//    QFileInfo fileInfo;
//    QString location;
//    location=QFileDialog::getExistingDirectory(this,tr(""),backupLocation);
//    fileInfo=QFileInfo(location);
//    ui->lineEdit->setText(fileInfo.filePath());
//    path=fileInfo.filePath();
    QFileDialog dialog(this,"");
    dialog.resize(580,400);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);
    dialog.setFileMode(QFileDialog::Directory);
    dialog.setDirectory(backupLocation);

    QStringList files = dialog.selectedFiles();
    if(!files.isEmpty())
    {
        if(dialog.exec() == QFileDialog::Accepted)
        {
            qDebug()<<"ok";
            QStringList name = dialog.selectedFiles();
            QString line = name.join("");
            ui->lineEdit->setText(line);
            path = line;
        }
        else
        {
            qDebug()<<"close";
        }
    }
}

void FileBackUpDialog::updateView()
{    
        ui->tableWidget->setRowCount(robotList.size());
        ui->tableWidget->setColumnCount(3);
        ui->tableWidget->setColumnWidth(0,30);
        ui->tableWidget->setColumnWidth(1,170);
        ui->tableWidget->setColumnWidth(2,210);
        ui->tableWidget->verticalHeader()->hide();
        QStringList header;
        header<<tr("")<<tr("机器人ID")<<tr("机器人名称");
        ui->tableWidget->setHorizontalHeaderLabels(header);
    for(int n=0;n<robotList.size();n++)
    {
        QString s=QString::number(robotList[n].RobotId);
        ui->tableWidget->setItem(n,0,new QTableWidgetItem());
        ui->tableWidget->setItem(n,1,new QTableWidgetItem(s));
        ui->tableWidget->setItem(n,2,new QTableWidgetItem(robotList[n].RobotName));
        QTableWidgetItem *checkBox = new QTableWidgetItem();
        checkBox->setCheckState(Qt::Unchecked);
        checkBox->setText("");
        ui->tableWidget->setItem(n,0, checkBox);
    }

    connect(ui->tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(cellChangedSlot(int, int)));
}

void FileBackUpDialog::changeLanguage()
{
    ui->retranslateUi(this);
}

void FileBackUpDialog::on_radioButton_clicked()
{

    //if( 0 == system("mount | grep udisk") )//有挂载
    if(globalData->usbFlag)
    {
        qDebug()<<"exist";
        backupLocation = CONTROLLER_USB_DISK_ADDRESS;
    }
    else
    {
        ui->radioButton->setChecked(false);
        ui->radioButton_2->setChecked(true);
        backupLocation = CONTROLLER_HARD_DISK_ADDRESS;

        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("未检测到U盘插入"));
    }

    //旧方案
//    QFile mFile("/agvwork/usb.data");
//    if(mFile.exists())
//    {
//        qDebug()<<"exist";
//        backupLocation=uDiskLocation;
//    }
//    else
//    {
//        ui->radioButton->setChecked(0);
//        ui->radioButton_2->setChecked(1);
//        backupLocation=hardDiskLocation;

//        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("未检测到U盘插入"));
//    }
}

void FileBackUpDialog::on_radioButton_2_clicked()
{
    backupLocation = CONTROLLER_HARD_DISK_ADDRESS;
}
